package com.example.courseapp.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.courseapp.R;
import com.example.courseapp.bean.ChildDietaryRecords;
import com.example.courseapp.bean.ChildSportRecords;
import com.example.courseapp.bean.FoodInfo;
import com.example.courseapp.bean.SportInfo;
import com.example.courseapp.utils.BigDecimalUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class SportRecordAdapter extends RecyclerView.Adapter<SportRecordAdapter.ViewHolder> {

    List<ChildSportRecords> courseList = new ArrayList<>();


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_record_item, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ChildSportRecords records = courseList.get(position);
//        List<SportInfo> infoList = new Gson().fromJson(records.getSport(),new TypeToken<List<SportInfo>>(){}.getType());
        List<SportInfo> infoList = records.newSport;
        StringBuilder str = new StringBuilder();
        for (SportInfo sportInfo : infoList) {
//            str.append("\n").append(foodInfo.getName()).append("  " + "半小时");
            String time = sportInfo.number > 1 ? ((BigDecimalUtils.div(String.valueOf(30 * sportInfo.number), "60", 1)) + "个小时") : "半个小时";
            String eng = sportInfo.number > 1 ? (BigDecimalUtils.mul(sportInfo.getEnergy(), String.valueOf(sportInfo.number), 1) + "能量") : sportInfo.getEnergy() + "能量";

            str.append("\n").append(sportInfo.getName()).append("  " + time).append("  " + eng);
        }
        holder.textView.setText(records.getCreateTime() + str + "\n");
    }

    @Override
    public int getItemCount() {
        return courseList.size();
    }

    public List<ChildSportRecords> getData() {
        return courseList;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void bindData(List<ChildSportRecords> courseList) {
        this.courseList.clear();
        this.courseList.addAll(courseList);
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}
